package br.com.data.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import br.com.arquitetura.data.GenericDao;
import br.com.model.entidade.Aula;
import br.com.model.entidade.Escola;
import br.com.model.entidade.Professor;
import br.com.model.entidade.Turma;
@Repository
public class TurmaHibernateDAO extends GenericDao {
	@Autowired
	public TurmaHibernateDAO(SessionFactory sessionFactory) {
		// TODO Auto-generated constructor stub
		setSessionFactory(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	public List<Turma> buscarAtivaPOrEscola(final Escola escola){
		
		
			return (List<Turma>) this.getHibernateTemplate().execute(
					new HibernateCallback() {

						// O HibernateCallback possibilita o acesso direto  API do
						// Hibernate para a execuo de consultas mais complexas que
						// no so cobertas pelo HibernateTemplate
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							Criteria c = session.createCriteria(Turma.class);
							
							c.add(Restrictions.eq("escola", escola));
							c.add(Restrictions.eq("ativo", true));
							
							
							c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);						
							
							return (c.list());
						}

					});
	}
}
